<?php
namespace App\Http\Controllers\Exp;
use App\Http\Controllers\Controller;
use App\Http\Controllers\ApiBaseController;
use Illuminate\Http\Request;
use App\Models\ExpModels\ExpCard;
use App\Components\QiniuTools;
class CardController extends ApiBaseController{
	/**
	*	爆款码管理模块
	*/
	
	public function __construct(Request $request) {
		parent::__construct($request);
		$this->params = $request->all();
	}
	

	
	//拉取视频列表
	function cardList(){
		$Card=new ExpCard();
		$partner_id=isset(session('vip_user')[0]['admin_id']) && intval(session('vip_user')[0]['admin_id']) ? intval(session('vip_user')[0]['admin_id']) : $this->returnJson(1,'请先登录');
		$title=isset($this->params['title']) ? $this->params['title'] : '';
		$nextPage=isset($this->params['nextPage']) && intval($this->params['nextPage']) ? intval($this->params['nextPage']) : 1;
		echo $Card->cardList($partner_id,$title,$nextPage);

	}

    //ajax拉取指定	活动页面的卡卷使用数据
    function getCardUserinfo()
    {
      //  isset(0('vip_user')[0]['admin_id']) ? '' : $this->returnJson(1, '请先登录');
        $Card = new Card();
        $cid = isset($this->params['id']) ? $this->params['id'] : 0;
        $page = isset($this->params['page']) && intval($this->params['page']) ? intval($this->params['page']) : 1;
        $re = $Card->getCardInfo($cid, $page);
        echo json_encode(["code" => 0, "html" => $re['html'], "nextPage" => $re['nextPage']]);
        exit;
    }

    //添加、修改卡券 --保存
	function save(){
		set_time_limit(0);
		$Card=new ExpCard();
		$Qiniu=new QiniuTools();
	    $kid=!empty($this->params['kid']) && intval($this->params['kid']) ? intval($this->params['kid']) : 0;
        $partner_id=!empty($this->params['pid']) && intval($this->params['pid']) ? intval($this->params['pid']) : 0;
		$imgData=isset($_FILES['img']['name']) && !empty($_FILES['img']['name']) ? $_FILES['img'] : false;
		!$kid && !$imgData ? $this->returnJson(1,'请上传图片') : '';
		
		$data['parnterId']=$partner_id;
		$data['title']=isset($this->params['title']) && $this->params['title'] ? htmlspecialchars($this->params['title'],ENT_QUOTES) : $this->returnJson(1,'请填写标签');
		$data['type']=isset($this->params['type']) && intval($this->params['type']) ? intval($this->params['type']) : $this->returnJson(1,'请选择类型');
		$data['price']=isset($this->params['price']) && $this->params['type']*100 ? $this->params['type']*100 : 0;
		$data['num']=isset($this->params['num']) && intval($this->params['num']) > 0 ? intval($this->params['num']) : -1;
//		$data['cill']=isset($this->params['cill']) && $this->params['cill'] ? htmlspecialchars($this->params['cill'],ENT_QUOTES) : '';
        $data['cill']=isset($this->params['cill']) && !empty($this->params['cill']) ? $this->params['cill'] : 0;
		$data['remark']=isset($this->params['remark']) && $this->params['remark'] ? htmlspecialchars($this->params['remark'],ENT_QUOTES) : '';
		if(empty($this->params['times'])){
			$this->returnJson(1,'请选择领取时间');
		}else{
			$data['staTime']=mb_substr($this->params['times'],0,19);
			$data['endTime']=mb_substr($this->params['times'],22);
		}
		
		if(empty($this->params['valid'])){
			$this->returnJson(1,'请选择有效时间');
		}else{
			$data['staValid']=mb_substr($this->params['valid'],0,19);
			$data['endValid']=mb_substr($this->params['valid'],22);
		}
		
		if($imgData){
			$imgType = isset($imgData['type']) && !empty($imgData['type']) ? $imgData['type'] : $this->returnJson(1, '图片不存在');
			$imgSize = isset($imgData['size']) && !empty($imgData['size']) ? $imgData['size'] : $this->returnJson(1, '图片不存在');
			if (!in_array($imgType, ['image/pjpeg', 'image/jpeg', 'image/png', 'image/x-png'])) {
				$this->returnJson(1, '图片格式错误');
			}
			if ($imgSize > 5242880) {
				$this->returnJson(1, '请上传小于5M的图片');
			}
			$data['url']=$Qiniu->upImg($imgData,"data/exp/card");
		}
		$data['updateTime']=date('Y-m-d H:i:s');
		
		if( $kid && !empty($data['img']) ){
			isset($this->params['oldImg']) && !empty($this->params['oldImg']) ? $Qiniu->delFile($this->params['oldImg']) : '';
		}
		
		// echo "<pre/>";print_r($data);
		$re=$Card->saveCard($kid,$data);
		$re ? $this->returnJson(0, '保存成功') : $this->returnJson(1, '保存失败');
	}
	
	//修改卡券状态
	function upStatus(){
		$Card=new ExpCard();

	    $kid=isset($this->params['kid']) && intval($this->params['kid']) ? intval($this->params['kid']) : 0;
        $partner_id=isset($this->params['pid']) && intval($this->params['pid']) ? intval($this->params['pid']) : 0;
		$status=$Card->selStatus($kid,$partner_id);
		$data['updateTime']=date('Y-m-d H:i:s');
		if($status==0){
			$data['status']=1;
		}elseif($status==1){
			$data['status']=0;
		}
		// echo "<pre/>";print_r($status);exit;
		$re=$Card->upStatus($kid,$data);
		$re ? $this->returnJson(0,'操作成功') : $this->returnJson(1,'操作失败');
	}
	
	//删除卡券
	function del(){
		$Card=new ExpCard();
		$kid=isset($this->params['kid']) && intval($this->params['kid']) ? intval($this->params['kid']) : 0;
		$re=$Card->del($kid);
		$re ? $this->returnJson(0,'操作成功') : $this->returnJson(1,'操作失败');
	}

	//视频相关方法
    //添加、修改视频 --保存
    function videoSave(){
        set_time_limit(0);
        $video=new ExpCard();
        $Qiniu=new QiniuTools();
        $partner_id=!empty($this->params['pid']) && intval($this->params['pid']) ? intval($this->params['pid']) : 0;
        $kid=!empty($this->params['kid']) && intval($this->params['kid']) ? intval($this->params['kid']) : 0;
        $imgData=isset($_FILES['img']['name']) && !empty($_FILES['img']['name']) ? $_FILES['img'] : false;
        $videoData=isset($_FILES['video']['name']) && !empty($_FILES['video']['name']) ? $_FILES['video'] : false;
        !$kid && !$videoData ? $this->returnJson(1,'请上传视频') : '';

        $data['parnterId']=$partner_id;
        $data['title']=isset($this->params['title']) && $this->params['title'] ? htmlspecialchars($this->params['title'],ENT_QUOTES) : $this->returnJson(1,'请填写标签');

        if($imgData){
            $imgType = isset($imgData['type']) && !empty($imgData['type']) ? $imgData['type'] : $this->returnJson(1, '图片不存在');
            $imgSize = isset($imgData['size']) && !empty($imgData['size']) ? $imgData['size'] : $this->returnJson(1, '图片不存在');
            if (!in_array($imgType, ['image/pjpeg', 'image/jpeg', 'image/png', 'image/x-png'])) {
                $this->returnJson(1, '图片格式错误');
            }
            if ($imgSize > 5242880) {
                $this->returnJson(1, '请上传小于5M的图片');
            }
            $data['img']=$Qiniu->upImg($imgData,"data/exp/img");
        }
        if($videoData){
            $videoType = isset($videoData['type']) && !empty($videoData['type']) ? $videoData['type'] : $this->returnJson(1, '视频不存在');
            $videoSize = isset($videoData['size']) && !empty($videoData['size']) ? $videoData['size'] : $this->returnJson(1, '视频不存在');
            if (!in_array($videoType, ['video/mp4', 'video/webm'])) {
                $this->returnJson(1, '只支持mp4、webm');
            }
            if ($videoSize > 52428800) {
                $this->returnJson(1, '请上传小于50M的视频');
            }
            $data['url']=$Qiniu->upVideo($videoData,"data/exp/video");
        }
        $data['updateTime']=date('Y-m-d H:i:s');

        if( $kid && !empty($data['img']) ){
            isset($this->params['oldImg']) && !empty($this->params['oldImg']) ? $Qiniu->delFile($this->params['oldImg']) : '';
        }
        if( $kid && !empty($data['url']) ){
            isset($this->params['oldVideo']) && !empty($this->params['oldVideo']) ? $Qiniu->delFile($this->params['oldVideo']) : '';
        }

        // echo "<pre/>";print_r($data);
        $re=$video->saveVideo($kid,$data);
        $re ? $this->returnJson(0, '保存成功') : $this->returnJson(1, '保存失败');
    }

    //修改视频状态
    function upVideoStatus(){
        $video=new ExpCard();
        $partner_id=!empty($this->params['pid']) && intval($this->params['pid']) ? intval($this->params['pid']) : 0;
        $kid=isset($this->params['kid']) && intval($this->params['kid']) ? intval($this->params['kid']) : 0;
        $status=$video->selVideoStatus($kid,$partner_id);
        $data['updateTime']=date('Y-m-d H:i:s');
        if($status==0){
            $data['status']=1;
        }elseif($status==1){
            $data['status']=0;
        }
        // echo "<pre/>";print_r($status);exit;
        $re=$video->upVideoStatus($kid,$data);
        $re ? $this->returnJson(0,'操作成功') : $this->returnJson(1,'操作失败');
    }

    //删除视频
    function videoDel(){
        $video=new ExpCard();
        $kid=isset($this->params['kid']) && intval($this->params['kid']) ? intval($this->params['kid']) : 0;
        $re=$video->Videodel($kid);
        $re ? $this->returnJson(0,'操作成功') : $this->returnJson(1,'操作失败');
    }
}